<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller;


use App\Model\User;
use App\Request\FooRequest;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Phper666\JwtAuth\Jwt;
use Phper666\JwtAuth\Middleware\JwtAuthMiddleware;

/**
 * @Controller()
 */
class UserController extends AbstractController
{
    /**
     * @GetMapping(path="index")
     */
    public function index(FooRequest $request)
    {
        $user   = $request->input('user', 'user');
        $method = $request->getMethod();

        return [
            'method'  => $method,
            'message' => "Hello {$user}.",
        ];
    }

    /**
     * @GetMapping(path="getUserBalance")
     * @return string
     * @author github_sw <github_sw@yeah.net>
     */
    public function getUserBalance(FooRequest $request)
    {
        return 'ppp';
    }

    /**
     * @PostMapping(path="store")
     */
    public function store()
    {
        return 'store';

    }


    /**
     * @DeleteMapping(path="destroy")
     */
    public function destroy()
    {
        return 'destroy';

    }

    /**
     * @PutMapping(path="edit")
     *  编辑
     */
    public function edit()
    {
        return 'edit';

    }

    /**
     * @GetMapping(path="show")
     */
    public function show(RequestInterface $request)
    {
        // 存在则返回，不存在则返回默认值 0
        $id = $request->input('id', 1);
        // 查询单个缓存
        $user = User::findFromCache($id);
        return [
            'user'  => $user,
        ];
    }


    /**
     * 模拟登录,获取token
     * @PostMapping(path="login")
     * @param Jwt $jwt
     * @return \Psr\Http\Message\ResponseInterface
     * @author github_sw <github_sw@yeah.net>
     */
    public function login(Jwt $jwt)
    {
        $username = $this->request->input('username');
        $password = $this->request->input('password');

        if ($username && $password) {
            //这里应为没有做auth的登录认证系统，为了展示随便写点数据
            $userData = [
                'uid' => 1,
                'username' => 'xx',
            ];
            //获取Token
            $token = (string)$jwt->getToken($userData);
            //返回响应的json数据
            return $this->response->json(['code' => 0, 'msg' => '获取token成功', 'data' => ['token' => $token]]);
        }

        return $this->response->json(['code' => 0, 'msg' => '登录失败', 'data' => []]);
    }

    /**
     * @GetMapping(path="getData")
     * @Middleware(JwtAuthMiddleware::class)
     * http头部必须携带token才能访问的路由
     * @return \Psr\Http\Message\ResponseInterface
     * @author github_sw <github_sw@yeah.net>
     */
    public function getData()
    {
        return $this->response->json(['code' => 0, 'msg' => 'success', 'data' => ['a' => 1]]);
    }

}
